﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.Elements
{
    public class HttpHeaderContentType
    {
        public string type = string.Empty;
        public string encoding = string.Empty;

        public HttpHeaderContentType(string value)
        {
            if (!value.Contains(";"))
                type = value.Trim();
            else if (value.Split(';').Length == 2)
            {
                if (value.Split(';')[1].Trim().StartsWith("charset="))
                {
                    type = value.Split(';')[0].Trim();
                    encoding = value.Split(';')[1].Trim().Substring("charset=".Length);
                }
            }
        }

        public override string ToString()
        {
            if (string.IsNullOrEmpty(encoding))
                return string.Format("Content-Type: {0}", type);
            else
                return string.Format("Content-Type: {0}; charset={1}", type, encoding);
        }

        public string GetValue()
        {
            if (string.IsNullOrEmpty(encoding))
                return string.Format("{0}", type);
            else
                return string.Format("{0}; charset={1}", type, encoding);
        }
    }
}
